{$A8,B-,C+,D+,E-,F-,G+,H+,I+,J-,K-,L+,M-,N+,O+,P+,Q-,R-,S-,T-,U-,V+,W-,X+,Y+,Z1}
{$WARN UNSAFE_TYPE OFF}

unit ExternalInterface;

interface

  uses
    Interface_TLB,
    ComObj,
    ExtActns;

  type
    TExternalInterface = class(TAutoIntfObject, IPootcherIntf, IDispatch)
      protected
        function Download(const url: WideString): OleVariant; safecall;
        function Execute(const command: WideString; hide: WordBool): LongWord; safecall;
        function Get_Path: WideString; safecall;
        function Get_Window: OleVariant; safecall;
        function openGrf(const grf_path: WideString): OleVariant; safecall;
        function openDatabase(const database_path: WideString): OleVariant; safecall;
        function openPath(const path: WideString): OleVariant; safecall;
        function createFolder(const folder_path: WideString): WordBool; safecall;
        function Get_Parameters: OleVariant; safecall;
        property Path: WideString read Get_Path;
        property Window: OleVariant read Get_Window;
        property Parameters: OleVariant read Get_Parameters;
      public
        constructor Create();
        destructor Destroy(); override;
    end;

implementation

  uses
    ActiveX,
    SysUtils,
    Variants,
    ProcessController,
    SQLiteWrap,
    ProcessRoutines,
    grf,
    grftypes,
    GrfInterface,
    DatabaseInterface,
    PathInterface,
    WindowInterface,
    ParameterInterface;

//*********************
//* TPootcherInterface
//*********************
  constructor TExternalInterface.Create();
  var
    TypeLib : ITypeLib;
    ExeName : WideString;
  begin
    //Get name of application
    ExeName := ParamStr(0);
    //Load type library from application's resources
    OleCheck(LoadTypeLib(PWideChar(ExeName), TypeLib));
    //Inherit
    inherited Create(TypeLib, IPootcherIntf);
  end;

  destructor TExternalInterface.Destroy();
  begin
    inherited;
  end;

  function TExternalInterface.Download(const url: WideString) : OleVariant;
  begin
    //Download(url, GetCurrentDir + '\' + ExtractUrlFileName(url), False);
    result := IDispatch(TProcessController.Create(url, cDownload));
  end;

  function TExternalInterface.Execute(const command: WideString; hide: WordBool): LongWord;
  begin
    Result := 0;//RunCommand(command, hide, nil);

  end;

  function TExternalInterface.openGrf(const grf_path: WideString): OleVariant;
  var
    Grf : PGrf;
    Error : TGrfError;
  begin
    Grf := nil;
    try
      Grf := grf_callback_open(PAnsiChar(Utf8Encode(grf_path)), 'r+b', Error, nil);
    finally
      if Grf <> nil then
        Result := IDispatch(TGrfInterface.Create(Grf))
      else
        Result := VarAsType(Result, VarEmpty);
    end;
  end;

  function TExternalInterface.openDatabase(const database_path: WideString): OleVariant;
  var
    Database : TSQLiteDatabase;
  begin
    try
      Database := TSQLiteDatabase.Create(Utf8Encode(database_path));
      Result := IDispatch(TDatabaseInterface.Create(Database));
    except
      Result := VarAsType(Result, VarEmpty);
    end;
  end;

  function TExternalInterface.openPath(const path: WideString): OleVariant;
  begin
    if DirectoryExists(path) or FileExists(path) then
      Result := IDispatch(TPathInterface.Create(path))
    else
      Result := VarAsType(Result, varEmpty);
  end;

  function TExternalInterface.createFolder(const folder_path: WideString): WordBool;
  begin
    if DirectoryExists(folder_path) then
      Result := false
    else
      try
        ForceDirectories(folder_path);
        Result := true;
      except
        Result := false;
      end;
  end;

  function TExternalInterface.Get_Path() : WideString;
  begin
    Result := GetCurrentDir;
  end;

  function TExternalInterface.Get_Window() : OleVariant;
  const
    {$J+}WindowInt : IDispatch = nil;{$J-}
  begin
    if WindowInt = nil then
      WindowInt := IDispatch(TWindowInterface.Create());
    Result := WindowInt;
  end;

  function TExternalInterface.Get_Parameters() : OleVariant;
  const
    {$J+}ParameterInt : IDispatch = nil;{$J-}
  begin
    if ParameterInt = nil then
      ParameterInt := IDispatch(TParameterInterface.Create());
    Result := ParameterInt;
  end;

end.
